﻿using Xceed.Wpf.Toolkit.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;  // for canceleventargs
using System.Runtime.InteropServices;
using System.Windows.Interop;
using Signage01.dao;


namespace Signage01.Dialogs
{
    /// <summary>
    /// Interaction logic for AddCampainWindow.xaml
    /// </summary>
    public partial class AddCampainWindow : Window
    {
        // some variables for window customization
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        //

        private List<Screen> listScreen;
        private string fileResource;
        private string fileExt;
        private string filePath;
        public AddCampainWindow(List<Screen> listScreen)
        {
            InitializeComponent();
            this.listScreen = listScreen;
            resCombobox.ItemsSource = listScreen;
        }

        private void buttonFinish_Click_1(object sender, RoutedEventArgs e)
        {
            Screen screenSelected = resCombobox.SelectedItem as Screen;

            if (screenSelected == null)
            {
                MessageBox.Show("No screen selected!");
                return;
            }


            if (textBoxCampaignName.Text.Trim() == "")
            {
                MessageBox.Show("Campaign Name empty!");
                return;
            }

            

            CampaignDAO campaignDAO = CampaignDAO.getSingleTon();

            List<Campaign> listCampaign = campaignDAO.getAll();

            

            Campaign campaign = new Campaign(screenSelected, textBoxCampaignName.Text);

            listCampaign.Add(campaign);

            campaignDAO.saveListToFile(listCampaign);

            /*
            fileResource = "resources\\campaignBackground\\" + textBoxCampaignName.Text + "." + fileExt;

            if (Directory.Exists("resources\\campaignBackground"))
            {
                File.Copy(filePath, fileResource, true);
            }
            */

            this.DialogResult = true;
            Close_Window();
        }

        private void resCombobox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

        }

        private void buttonCancel_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            Close_Window();
        }

        private void Close_Window()
        {
            this.Owner.ShowActivated = true;
            this.Close();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            var hwnd = new WindowInteropHelper(this).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
        }

        /*
        private void buttonImageUploadCampaignBackground_Click_1(object sender, RoutedEventArgs e)
        {
            if (textBoxCampaignName.Text.Trim() == "")
            {
                MessageBox.Show("First give the campaign name!");
                return;
            }
            
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();

            openFileDialog.Filter = "Image files (.bmp,.jpg,.png) |*.bmp;*.jpg;*.png";

            Nullable<bool> result = openFileDialog.ShowDialog();

            if (result == true)
            {
                //string fileName = openFileDialog.FileName;                
                filePath = openFileDialog.FileName;
                string[] splittedString = filePath.Split('\\');

                string fileNameWithExt = splittedString[splittedString.Count() - 1];
                string[] splittedFileName = fileNameWithExt.Split('.');
                fileExt = splittedFileName[1];

                BitmapImage tempImage = new BitmapImage();
                tempImage.BeginInit();
                tempImage.UriSource = new Uri(filePath);
                tempImage.EndInit();

                imageCampaignBackground.Source = tempImage;
            }
        }
         */
    }
}
